<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once(BASEPATH."application/config/config.php") ;
class Page
{

    var $js_version = 0;

    //
    //	Links section
    //
    var $js_links = array();
    var $css_links = array();

    //
    //	Javascript integration section
    //
    var $js_inits = array();
    var $js_data = array();

    //
    //	Meta section
    //
    var $title = '';
    var $description = '';
    var $keywords = '';

    function Page()
    {

        $CI = &get_instance();

        $CI->config->load('page', true);

        $default = $CI->config->item('page');
        //print_r($default);die();
        $this->title = isset($default['page']['title']) ? $default['page']['title'] : '';
        $this->description = isset($default['page']['desc']) ? $default['page']['desc'] :
            '';
        $this->tab = isset($default['page']['js_version']) ? $default['page']['js_version'] :
            '';
        $this->tab = isset($default['page']['css_links']) ? $default['page']['css_links'] :
            array();
        $this->tab = isset($default['page']['js_links']) ? $default['page']['js_links'] :
            array();

    }
    function cheat_url()
    {
        $CI = &get_instance();
        return $CI->config->slash_item('cheat_url');
    }
    ///untuk loading theme biar enak
    function loadTheme($asset_name, $theme_name = 'blue', $asset_type = null)
    {
        $base_url = $this->cheat_url();
        $asset_location = $base_url . 'theme/';

        $asset_location .= $theme_name . '/';

        $asset_location .= $asset_type . '/' . $asset_name;
        return $asset_location;
    }

    function LoadCss($asset_name, $theme_name = 'blue')
    {
        return $this->loadTheme($asset_name, $theme_name, 'css');
    }

    function LoadImage($asset_name, $theme_name = 'blue')
    {
        return $this->loadTheme($asset_name, $theme_name, 'images');
    }
    function LoadJs($asset_name, $theme_name = 'blue')
    {
        return $this->loadTheme($asset_name, $theme_name, 'js');
    }
    //
    //	Add a single script (or array of scripts) to the page
    //
    function js_link($script)
    {

        $this->_extend($this->js_links, $this->LoadJs($script));

        return $this;

    }

    //
    //	Add a single stylesheet (or array of stylesheet) to the page
    //
    function css_link($css)
    {

        $this->_extend($this->css_links, $this->LoadCss($css));

        return $this;

    }

    //
    //	Add a single (or an array of) lines to the window.onload function
    //
    function js_init($line)
    {

        $this->_extend($this->js_inits, $line);

        return $this;

    }

    //
    //	Set a data item in the javascript data list
    //	Pass it key, value pair or a single array of key, value pairs
    //
    function js_set($key, $value = null)
    {

        if (is_array($key)) {

            $this->js_data = array_merge($this->js_data, $key);

        } else {

            $this->js_data[$key] = $value;

        }

        return $this;

    }

    //
    //	Set the page title
    //
    function set_title($title)
    {
        $this->title = $title;

        return $this;
    }

    //
    //	Set the page description
    //
    function set_description($description)
    {
        $this->description = $description;

        return $this;
    }

    //
    //	Set the page keywords
    //
    function set_keywords($keywords)
    {
        $this->keywords = $keywords;

        return $this;
    }


    //	------------------------------------------------------------------------------------
    //	Generation section
    //	------------------------------------------------------------------------------------

    function do_head()
    {

        //
        //	If the form helper is loaded, prep the meta data.
        //
        if (function_exists('form_prep'))
            $this->description = form_prep($this->description);
        if (function_exists('form_prep'))
            $this->keywords = form_prep($this->keywords);


        if (!empty($this->title))
            echo "<title>$this->title</title>\n";
        if (!empty($this->description))
            echo "<meta name=\"description\" content=\"$this->description\"/>\n";
        if (!empty($this->keywords))
            echo "<meta name=\"keywords\" content=\"$this->keywords\"/>\n";

        $this->_do_css_links();
        $this->_do_js_links();

        $this->_do_js_data();
        $this->_do_js_init();

    }


    function _do_js_links()
    {

        $this->_do_links($this->js_links,
            '<script type="text/javascript" src="/ilkom/%s"></script>', true);

    }

    function _do_css_links()
    {

        $this->_do_links($this->css_links,
            '<link rel="stylesheet" type="text/css" media="all" href="/ilkom/%s" />', true);

    }

    function _do_js_data()
    {

        if (count($this->js_data) == 0)
            return;

        echo "<script type=\"text/javascript\">\n/* <![CDATA[ */\n";

        foreach ($this->js_data as $key => $value) {

            $value = json_encode($value);

            echo "var $key=$value;\n";

        }

        echo "/* ]]> */\n</script>\n";

    }

    function _do_js_init()
    {

        if (count($this->js_inits) == 0)
            return;

        echo "<script type=\"text/javascript\">\n/* <![CDATA[ */\nwindow.onload=function(){\n";

        foreach ($this->js_inits as $line) {

            echo $line . "\n";

        }

        echo "}\n/* ]]> */\n</script>\n";

    }

    //	------------------------------------------------------------------------------------
    //	Utility section
    //	------------------------------------------------------------------------------------

    function _do_links(&$links, $template, $versionise = false)
    {

        if (count($links) == 0)
            return;

        foreach ($links as $link) {

            if ($versionise)
                $link = $this->_version($link);

            echo (sprintf($template, $link) . "\n");

        }

    }

    //
    //	Utility function to add ?v=<version> to the end of local script/css URLs. If js_version=0 nothing is added.
    //
    function _version($url)
    {

        if ((substr($url, 0, 1) == '/') && ($this->js_version > 0))
            $url = $url . '?v=' . $this->js_version;

        return $url;

    }

    function _extend(&$array, $with)
    {

        if (is_array($with)) {

            $array = array_merge($array, $with);

        } else {

            $array[] = $with;

        }

    }

}
